var member_id  = ws = flea = '';
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	member_id = plus.storage.getItem('id');
	plus.nativeUI.showWaiting('加载中...');
	console.log()
	$.ajax({ 
		type:"post",
		url:apiRoot + "?act=member_order&op=order_list",
		data:{member_id:member_id,ordertype:1},
		dataType:'json',
		success:function(info){
//			console.log(JSON.stringify(info)	);
			plus.nativeUI.closeWaiting();
			if(info){
				var html1 = '';
				var html2 = '';
				var html3 = '';
				var html4 = '';
				var html5 = '';
//				var html1 = ''; 
				var zt = '<div style="width: 80%;margin: 40px auto;text-align: center;font-size: 18px;">你还没有数据...</div>';
//				var html2 = html3 = html4 = html5 = '<ul class="mui-table-view mui-grid-view">';
				if(info.datas.order_group_list.length > 0){
					$.each(info.datas.order_group_list, function(k1,v1) {
						$.each(v1.order_list, function(k2,v2) {
							html1 += '<ul class="mui-table-view mui-grid-view marg"><div class="order_goods" data-order_id="'+ v2.order_id +'" data-evaluation_state="'+ v2.evaluation_state +'">';
							$.each(v2.extend_order_goods, function(k,v) {
								html1 += '<li class="mui-table-view-cell mui-col-xs-3">'+
												'<img class="mui-media-object mui-pull-left" src="'+ v.goods_image_url +'"/>'+
										 '</li>'+
										 '<li class="mui-table-view-cell mui-col-xs-9">'+
											 '<div class="mui-ellipsis-2">'+ v.goods_name +'</div>'+										
											 '<div class="money">'+
												'<span>￥'+ v.goods_price +'</span>'+
												'<span>x'+ v.goods_num +'</span>'+
											 '</div>'+
										 '</li>';
							});
							switch(v2.order_state){ 
								case '10' :
								    html1 += '</div><li class="mui-table-view-cell total">'+									
												'<span>合计：￥'+ v2.goods_amount +'</span>'+
												'<span>'+
												  '<button type="button" class="mui-btn order_cancel" data-order_id="'+ v2.order_id +'">取消订单</button>'+
												  '<button type="button" class="mui-btn RGB payment" data-order_id="'+ v2.order_id +'" data-goods_total="'+ v2.goods_amount +'" data-order_sn="'+ v2.order_sn +'">付款</button>'+
											 	'</span>'+
											 '</li>'+
											'</ul>';
								break;
								case '20' :
									html1 += '</div><li class="mui-table-view-cell total">'+									
												'<span>合计：￥'+ v2.goods_amount +'</span>'+
												'<span>'+
												  '<button type="button" class="mui-btn" onclick="openNewPage(orders_delivery.html)">配送中</button>'+
											 	'</span>'+
											 '</li>'+
											'</ul>';
								break;
								case '30' :
									html1 += '</div><li class="mui-table-view-cell total">'+									
												'<span>合计：￥'+ v2.goods_amount +'</span>'+
												'<span>'+
												  '<button type="button" class="mui-btn RGB order_receive" data-order_id="'+ v2.order_id +'">确认收货</button>'+
											 	'</span>'+
											 '</li>'+
											'</ul>';
								break;  
								case '40' :
									html1 += '</div><li class="mui-table-view-cell total">'+									
												'<span>合计：￥'+ v2.goods_amount +'</span>'+
												'<span>';
												if(v2.evaluation_state == 1){
													 html1 +=  '<button type="button" class="mui-btn">已评价</button>';
												}else{ 
													 html1 +=  '<button type="button" class="mui-btn RGB evaluate" data-order_id="'+ v2.order_id +'">评价</button>';
												}
											 	html1 +=  '</span>'+
											 '</li>'+
											'</ul>';
								break;
								case '0' :
									html1 += '</div><li class="mui-table-view-cell total">'+									
												'<span>合计：￥'+ v2.goods_amount +'</span>'+
												'<span>'+
												  '<button type="button" class="mui-btn">已取消</button>'+
											 	'</span>'+
											 '</li>'+
											'</ul>';
								break;
							}
						});
					});
					$('#item1mobile .mui-scroll').html(html1);
				}else{
					$('#item1mobile .mui-scroll').html(zt);
				}
				var statea = stateb = statec = stated = 0;
				$.each(info.datas.order_group_list, function(k1,v1) {
					$.each(v1.order_list, function(k2,v2) {
						switch(v2.order_state){
							case '10' :
								statea++;
								html2 += '<ul class="mui-table-view mui-grid-view marg"><div class="order_goods" data-order_id="'+ v2.order_id +'" data-evaluation_state="'+ v2.evaluation_state +'">';
								$.each(v2.extend_order_goods, function(k,v) {
									html2 += '<li class="mui-table-view-cell mui-col-xs-3">'+
													'<img class="mui-media-object mui-pull-left" src="'+ v.goods_image_url +'"/>'+
												'</li>'+
												'<li class="mui-table-view-cell mui-col-xs-9">'+
													'<div class="mui-ellipsis-2">'+ v.goods_name +'</div>'+										
													'<div class="money">'+
														'<span>￥'+ v.goods_price +'</span>'+
														'<span>x'+ v.goods_num +'</span>'+
													'</div>'+
												'</li>';
								});
							    html2 += '</div><li class="mui-table-view-cell total">'+									
											'<span>合计：￥'+ v2.goods_amount +'</span>'+
											'<span>'+
											  '<button type="button" class="mui-btn order_cancel" data-order_id="'+ v2.order_id +'">取消订单</button>'+
											  '<button type="button" class="mui-btn RGB payment" data-order_id="'+ v2.order_id +'" data-goods_total="'+ v2.goods_amount +'" data-order_sn="'+ v2.order_sn +'">付款</button>'+
										 	'</span>'+
										 '</li>'+
										'</ul>';
							break;
							case '20' :
								stateb++;
								html3 += '<ul class="mui-table-view mui-grid-view marg"><div class="order_goods" data-order_id="'+ v2.order_id +'">';
								$.each(v2.extend_order_goods, function(k,v) {
									html3 += '<li class="mui-table-view-cell mui-col-xs-3">'+
													'<img class="mui-media-object mui-pull-left" src="'+ v.goods_image_url +'"/>'+
												'</li>'+
												'<li class="mui-table-view-cell mui-col-xs-9">'+
													'<div class="mui-ellipsis-2">'+ v.goods_name +'</div>'+										
													'<div class="money">'+
														'<span>￥'+ v.goods_price +'</span>'+
														'<span>x'+ v.goods_num +'</span>'+
													'</div>'+
												'</li>';
								});
								html3 += '</div><li class="mui-table-view-cell total">'+									
											'<span>合计：￥'+ v2.goods_amount +'</span>'+
											'<span>'+
											  '<button type="button" class="mui-btn" onclick="openNewPage(orders_delivery.html)">配送中</button>'+
										 	'</span>'+
										 '</li>'+
										'</ul>';
							break;
							case '30' :
								statec++;
								html4 += '<ul class="mui-table-view mui-grid-view marg"><div class="order_goods" data-order_id="'+ v2.order_id +'">';
								$.each(v2.extend_order_goods, function(k,v) {
										html4 += '<li class="mui-table-view-cell mui-col-xs-3">'+
														'<img class="mui-media-object mui-pull-left" src="'+ v.goods_image_url +'"/>'+
													'</li>'+
													'<li class="mui-table-view-cell mui-col-xs-9">'+
														'<div class="mui-ellipsis-2">'+ v.goods_name +'</div>'+										
														'<div class="money">'+
															'<span>￥'+ v.goods_price +'</span>'+
															'<span>x'+ v.goods_num +'</span>'+
														'</div>'+
													'</li>';
								}); 
								html4 += '</div><li class="mui-table-view-cell total">'+									
											'<span>合计：￥'+ v2.goods_amount +'</span>'+
											'<span>'+
											  '<button type="button" class="mui-btn RGB order_receive" data-order_id="'+ v2.order_id +'">确认收货</button>'+
										 	'</span>'+
										 '</li>'+
										'</ul>';
							break;  
							case '40' :
								stated++;
								html5 += '<ul class="mui-table-view mui-grid-view marg"><div class="order_goods" data-order_id="'+ v2.order_id +'">';
								$.each(v2.extend_order_goods, function(k,v) {
										html5 += '<li class="mui-table-view-cell mui-col-xs-3">'+
														'<img class="mui-media-object mui-pull-left" src="'+ v.goods_image_url +'"/>'+
													'</li>'+
													'<li class="mui-table-view-cell mui-col-xs-9">'+
														'<div class="mui-ellipsis-2">'+ v.goods_name +'</div>'+										
														'<div class="money">'+
															'<span>￥'+ v.goods_price +'</span>'+
															'<span>x'+ v.goods_num +'</span>'+
														'</div>'+
													'</li>';
								});
								html5 += '</div><li class="mui-table-view-cell total">'+									
											'<span>合计：￥'+ v2.goods_amount +'</span>'+
											'<span>';
											if(v2.evaluation_state == 1){
												 html5 +=  '<button type="button" class="mui-btn">已评价</button>';
											}else{
												 html5 +=  '<button type="button" class="mui-btn RGB evaluate" data-order_id="'+ v2.order_id +'">评价</button>';
											}
										 	html5 +=  '</span>'+
										 '</li>'+
										'</ul>';
							break;
						}
					});
				});
				if(statea > 0){
					$('#item2mobile .mui-scroll').html(html2);
				}else{
					$('#item2mobile .mui-scroll').html(zt);
				}
				if(stateb > 0){
					$('#item3mobile .mui-scroll').html(html3);
				}else{
					$('#item3mobile .mui-scroll').html(zt);
				}
				if(statec > 0){
					$('#item4mobile .mui-scroll').html(html4);
				}else{
					$('#item4mobile .mui-scroll').html(zt);
				}
				if(stated > 0){
					$('#item5mobile .mui-scroll').html(html5); 
				}else{
					$('#item5mobile .mui-scroll').html(zt); 
				} 
			}
			//取消订单
			$('.order_cancel').on('click',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');
				order_cancel(member_id,order_id);
			})
			//确认收货
			$('.order_receive').on('click',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');
				order_receive(member_id,order_id);
			})
			//订单详情
			$('.order_goods').unbind('tap');
			$('.order_goods').on('tap',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');
				var evaluation_state = _this.attr('data-evaluation_state');
				var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
				var webviewContent= plus.webview.create('./orders_goods.html','orders_goods.html',{},{order_id:order_id,spage:ws.id,evaluation_state:evaluation_state});//后台创建webview并打开show.html
				webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
			        nwaiting.close(); //新webview的载入完毕后关闭等待框
			        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
			    }, false);
			})
			//评价
			$('.evaluate').unbind('tap');
			$('.evaluate').on('tap',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');
				var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框
				var webviewContent= plus.webview.create('./Evaluation.html','Evaluation.html',{},{order_id:order_id,spage:ws.id,zt:1});//后台创建webview并打开show.html
				webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
			        nwaiting.close(); //新webview的载入完毕后关闭等待框
			        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
			    }, false);
			})
			//付款
			$('.payment').on('click',function(){
				var _this = $(this);
				var order_id = _this.attr('data-order_id');   //订单id
				var order_sn = _this.attr('data-order_sn');   //订单号
				var goods_total = _this.attr('data-goods_total');   //支付金额
				plus.nativeUI.actionSheet({cancel:"取消",buttons:[{title:"余额支付"},{title:"支付宝支付"},{title:"微信支付"}]},function(e){
					if(e.index == 1){
						$('.password-of-pay-wraper').css("display","block");
							$('#password_backdrop').css("display","block");
							$('.btn').slideUp()				
							
							//如果用户选择的是余额支付，则执行如下代码
							tool.shade(function (){
								$("#j_password_of_pay").hide();
							}).show();  
							//默认让第一个输入框获得焦点
							setTimeout(function (){
								$("#j_password_of_pay").show().find("input:password").eq(0).focus();
							},10); 
							var pwdInputs = $("#j_password_of_pay input:password");
							pwdInputs.each(function (index,item){
								$(this).on("keyup",function (){
									if(!tool.isEmpty($(this).val())){
										$(this).next().focus();
										//如果是最后一个输入框，则执行ajax操作
										if(index == pwdInputs.size() - 1){
											var pwd = "";
											$(this).blur();
											pwdInputs.each(function (){
												pwd += $(this).val();
											}); 
											yuezhifu(order_id,pwd);
			//									alert("用户输入的密码是："+ pwd +"，发送ajax，检查用户输入密码是否正确");	
			//									$("#j_password_of_pay").hide();
			//									tool.shade().hide();
			//									$('.popover').fadeOut();
										}
									}
								});
							});
						
					}else if(e.index == 2){
						ptype = 'alipay';
						payment(order_sn,goods_total);
					}else if(e.index == 3){
						ptype = 'wxpay';
						payment(order_sn,goods_total);
					}else{
						toast("取消支付");
					}
				});
				
			})
			
		},
		error:function(e){
			plus.nativeUI.closeWaiting(); 
			toast('连接失败！');
		}
	});
	
},false)